<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Meizu
 *
 * @author dragon.hu add @2016.2.17
 */
require_once 'BaseChannel.php';

//魅族
class Meizu implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="KkwzdkBoYfFC5wjOKcVrrcjPH2Q3z0V0"; // AppSecret
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey="KkwzdkBoYfFC5wjOKcVrrcjPH2Q3z0V0"; // AppSecret
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("meizu", $_REQUEST);
		
		$notify_time = $_REQUEST['notify_time'];  //通知发送时间
		$notify_id = $_REQUEST['notify_id'];  //通知id
		$order_id = $_REQUEST['order_id'];  //订单id
		$app_id = $_REQUEST['app_id'];  //应用id
		$uid = $_REQUEST['uid'];  //用户id
		$partner_id = $_REQUEST['partner_id'];  //商户id
		$cp_order_id = $_REQUEST['cp_order_id'];  //游戏订单id
		$product_id = $_REQUEST['product_id'];  //产品id
		$product_unit = $_REQUEST['product_unit'];  //产品单位
		$buy_amount = $_REQUEST['buy_amount'];  //购买数量
		$product_per_price = $_REQUEST['product_per_price'];  //产品单价
		$total_price = $_REQUEST['total_price'];  //购买总价
		$trade_status = $_REQUEST['trade_status'];  //交易状态(1.待支付，2.支付中，3.已支付，4.取消订单，5.未知异常取消订单)
		$create_time = $_REQUEST['create_time'];  //订单时间
		$pay_time = $_REQUEST['pay_time'];  //支付时间
		$pay_type = $_REQUEST['pay_type'];  //支付类型(1.不定金额充值，0.购买)
		$user_info = $_REQUEST['user_info'];  //用户自定义信息
		$sign = $_REQUEST['sign'];  //参数签名
		$sign_type = $_REQUEST['sign_type'];  //签名类型
		
		/*签名格式：app_id=464013&buy_amount=1&cp_order_id=2680&create_time=1413776092239&notify_id=1413776
		113206&notify_time=2014-10-20 11:35:13&order_id=14102000000298934&partner_id=5458428&
		pay_time=1413776113219&pay_type=0&product_id=2&product_per_price=1.0&product_unit= 个
		&total_price=1.0&trade_status=3&uid=9700193&user_info=其它回传附加信息:appSecret
		*/
		$mysign = "app_id=".$app_id."&buy_amount=".$buy_amount."&cp_order_id=".$cp_order_id."&create_time=".$create_time.
		"&notify_id=".$notify_id."&notify_time=".$notify_time."&order_id=".$order_id."&partner_id=".$partner_id."&pay_time=".$pay_time.
		"&pay_type=".$pay_type."&product_id=".$product_id."&product_per_price=".$product_per_price."&product_unit=".$product_unit.
		"&total_price=".$total_price."&trade_status=".$trade_status."&uid=".$uid."&user_info=".$user_info.":".$paymentKey;
		
		
		if($sign == md5($mysign))  
		{ //验证通过
			//$status = STATUS_SUCCESS;
			//if ($trade_status != 1)  //交易状态(1.待支付，2.支付中，3.已支付，4.取消订单，5.未知异常取消订单)
			//{
			//  	$status = STATUS_FAILD;		
			//}
			if(false == $orderLog->recordOrder(STATUS_SUCCESS, $total_price, $cp_order_id, "meizu", $platform,$order_id))
			{
				$result['code'] = '120014';
				$result['message'] = urlencode("发货失败");
				$orderLog->log("meizu",array("order"=>$cp_order_id,"money"=>$total_price,"serverresult"=>"faild"));
				//echo " insert order:$ext error ";
			}else 
			{
				//支付成功
				$result['code'] = '200';
				$result['message'] = urlencode("成功发货");
				$orderLog->log("meizu",array("order"=>$cp_order_id,"money"=>$total_price,"serverresult"=>"success"));
			}
			 
		}
		else
		{
			$result['code'] = '120014';
			$result['message'] = urlencode("sign验证错误");
			$orderLog->log("meizu","  order:$cp_order_id  sign error sign:$sign mysign:$mysign");
		}
		$Res = json_encode($result);
		return urldecode($Res);
	}
	
}